import 'dart:math';
import 'app_index.dart';

import 'package:flutter/material.dart';
import '../until/load_image.dart';

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with RestorationMixin {
  final List<String> _titls = ['第一', '第二', '第三', '第四'];
  final List<String> _iconImageArray = [
    'tab_setting',
    'tab_setting',
    'tab_setting',
    'tab_setting'
  ];
  final List<String> _iconSelectImageArray = [
    'tab_setting_s',
    'tab_setting_s',
    'tab_setting_s',
    'tab_setting_s'
  ];

  List<Widget> _subPage = [];
  PageController _pageControll = PageController();
  List<BottomNavigationBarItem> items = [];

  final RestorableInt _counter = RestorableInt(0);

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    items = createItems();
    _subPage = [
      AppIndex(),
      Container(
        color: Colors.green,
      ),
      Container(
        color: Colors.yellow,
      ),
      Container(
        color: Colors.purple,
      ),
    ];
  }

  List<BottomNavigationBarItem> createItems() {
    return List.generate(
      _titls.length,
      (index) => BottomNavigationBarItem(
          icon: LoadAssetImage(
            _iconImageArray[index],
            width: 21,
            height: 21,
          ),
          activeIcon: LoadAssetImage(
            _iconSelectImageArray[index],
            width: 21,
            height: 21,
          ),
          label: _titls[index]),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        backgroundColor: Colors.white,
        currentIndex: _counter.value,
        selectedFontSize: 11,
        unselectedFontSize: 11,
        selectedItemColor: Color.fromARGB(255, 17, 39, 179),
        unselectedItemColor: Color.fromARGB(255, 166, 166, 166),
        iconSize: 11,
        items: items,
        onTap: (index) {
          _counter.value = index;
          _pageControll.jumpToPage(index);
          setState(() {});
        },
      ),
      body: PageView(
        controller: _pageControll,
        children: _subPage,
        physics: const NeverScrollableScrollPhysics(), // 禁止滑动
      ),
    );
  }

  @override
  // TODO: implement restorationId
  String? get restorationId => 'home_page';

  @override
  void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
    registerForRestoration(_counter, 'bottom_nav_bar_index');
  }
}
